//
//  LCTabbar.m
//  LuoChang
//
//  Created by Rick on 15/4/29.
//  Copyright (c) 2015年 Rick. All rights reserved.
//

#import "LCTabbar.h"
#import "LCTabBarController.h"

@interface LCTabbar()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    LCTabBarButton *_selectedBarButton;
}
@end

@implementation LCTabbar
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addBarButtons];
    }
    return self;
}

-(void) addBarButtons{
    
    NSArray * imgs = @[@"but_lx_n",@"but_brokerage_n",@"but_shopcart_n",@"but_my_n"];
    NSArray * selimgs = @[@"but_lx_p",@"but_brokerage_p",@"but_shopcart_p",@"but_my_p"];
    for (int i = 0 ; i<5 ; i++) {
        LCTabBarButton *btn = [[LCTabBarButton alloc] init];
        CGFloat btnW = self.frame.size.width/5;
        CGFloat btnX = i * btnW;
        CGFloat btnY = 0;
        
        CGFloat btnH = self.frame.size.height;
        
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        NSString *imageName = imgs[i>1?i-1:i];
        NSString *selImageName = selimgs[i>1?i-1:i];
        NSString *title;
        if (i==0) {
            title = @"绿稀";
        }else if(i==1){
            title = @"分佣";
        }else if(i==2){
            imageName = @"加号";
            selImageName =@"加号";
        }else if(i==3){
            title = @"购物车";
        }else if(i==4){
            title = @"个人中";
        }
        [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:selImageName] forState:UIControlStateSelected];
        btn.tag = i;
            [btn setTitle:title forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize: 11.0];
            btn.titleLabel.textAlignment = NSTextAlignmentCenter;
            [btn setTitleColor:RGB(79, 167, 46) forState:UIControlStateSelected];
            [btn setTitleColor:RGB(128, 128, 128) forState:UIControlStateNormal];
            [self addSubview:btn];
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
        
        btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        if(i == 0){
            [self btnClick:btn];
        }
    }
}


-(void) btnClick:(UIButton *)button{
    if (button.tag!=2) {
        [self.delegate changeNav:_selectedBarButton.tag to:button.tag>1?button.tag-1:button.tag];
        _selectedBarButton.selected = NO;
        button.selected = YES;
        _selectedBarButton = (LCTabBarButton *)button;
    }else{
        
        
    }
    
}


@end
